package com.hua.common.commonview;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.hua.common.commontools.DipUtil;
/**
 * 下拉刷新控件
 * @author huaxiaolin_pc
 *
 */
@SuppressLint("ClickableViewAccessibility")
public class RefreshLayout extends RelativeLayout {

	/**
	 * 子控件
	 */
	private View childViews = null;
	/**
	 * 下拉的距离
	 */
	private float subY = 0;
	/**
	 * } 按下的位置
	 */
	private float mTouchStartY;
	private float mTouchCurY;
	/**
	 * 停留的高度
	 */
	private float mPullHeight;
	/**
	 * 下拉刷新头控件实现的接口
	 */
	private HeardViewRefreshListener heardRefreshListener = null;
	/**
	 *
	 */
	private MyRefreshListener listener = null;
	/*
	 * 下拉刷新的头控件
	 */
	private View hearView = null;
	/**
	 * 控件是否正在刷新
	 */
	private boolean isRefreshing = false;
	/**
	 * 是否是此控件自带的头控件
	 */
	private boolean isNativeHeardView = true;

	public RefreshLayout(Context context) {
		super(context);
		this.init();
	}

	public RefreshLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.init();
	}

	public RefreshLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.init();
	}

	private void init() {
		this.initViews();
	}

	/**
	 * 初始化控件view
	 */
	private void initViews() {
		mPullHeight = DipUtil.Dp2px(getContext(), 60f);
		RefreshHeardView progressControl = new RefreshHeardView(getContext());
		progressControl.setBackgroundColor(Color.parseColor("#ffffffff"));
		LayoutParams params = new LayoutParams((int) mPullHeight,
				(int) mPullHeight);
		params.addRule(RelativeLayout.CENTER_HORIZONTAL);
		View childView = this.getChildAt(0);
		if (childView != null) {
			this.removeViewAt(0);
		}
		this.addView(progressControl, 0, params);
		this.heardRefreshListener = (HeardViewRefreshListener) progressControl;
		this.isNativeHeardView = true;
	}

	/**
	 * 设置头部控件 此view必须实现HeardheardRefreshListener接口
	 *
	 * @param heardView
	 */
	public void setHeardView(View heardView) {
		if (heardView == null) {
			initViews();
		} else {
			this.hearView = heardView;
			this.hearView.setBackgroundColor(Color.parseColor("#ffffffff"));
			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			params.addRule(RelativeLayout.CENTER_HORIZONTAL);
			this.removeViewAt(0);
			this.addView(this.hearView, 0, params);
			this.heardRefreshListener = (HeardViewRefreshListener) heardView;
			this.isNativeHeardView = false;
		}
	}

	/**
	 * 返回控件当前是否使用的是自带的heardview
	 *
	 * @return
	 */
	public boolean getIsNativeHeardView() {
		return isNativeHeardView;
	}

	/**
	 * 设置下拉刷新回调接口
	 */
	public void setRefreshListener(MyRefreshListener listener) {
		this.listener = listener;
	}

	private ValueAnimator initAnim(float subY) {
		ValueAnimator animator = ValueAnimator.ofFloat(subY, mPullHeight);
		animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				float top = (float) animation.getAnimatedValue();
				move(childViews, (int) top);
			}
		});
		animator.setDuration(300);
		animator.addListener(new Animator.AnimatorListener() {
			@Override
			public void onAnimationStart(Animator animation) {
			}

			@Override
			public void onAnimationRepeat(Animator animation) {
			}

			@Override
			public void onAnimationEnd(Animator animation) {
				heardRefreshListener.reFreshing();
				if (listener != null) {
					listener.reFreshing();
				}
			}

			@Override
			public void onAnimationCancel(Animator animation) {
			}
		});
		return animator;
	}

	/**
	 * 在此方法中获取子控件
	 */
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		childViews = getChildAt(1);
	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		if (isRefreshing) {
			return super.onInterceptTouchEvent(ev);
		}
		switch (ev.getAction()) {
			case MotionEvent.ACTION_DOWN:
				mTouchStartY = ev.getY();
				mTouchCurY = mTouchStartY;
				break;
			case MotionEvent.ACTION_MOVE:
				if (ev.getY() - mTouchStartY > 3 && !canChildScrollUp()) {
					return true;
				}
		}
		return super.onInterceptTouchEvent(ev);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (isRefreshing) {
			return super.onTouchEvent(event);
		}
		switch (event.getAction()) {
			case MotionEvent.ACTION_MOVE:
				mTouchCurY = event.getY();
				float dy = mTouchCurY - mTouchStartY;
				dy = Math.min(mPullHeight * 2, dy);
				dy = Math.max(0, dy);
				subY = dy;
				if (subY >= 0) {
					move(childViews, (int) subY);
				} else {
					move(childViews, 0);
				}
				this.heardRefreshListener.move(subY, mPullHeight * 2);
				return true;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				if (subY > mPullHeight) {
					isRefreshing = true;
					this.initAnim(subY).start();
				} else {
					close(subY);
				}
				subY = 0;
				this.heardRefreshListener.up();
				return true;
			default:
				return super.onTouchEvent(event);
		}
	}

	/**
	 * 用于使控件向下移动的方法
	 *
	 * @param view
	 * @param top
	 */
	private void move(View view, int top) {
		LayoutParams layoutparams = (LayoutParams) view.getLayoutParams();
		layoutparams.setMargins(0, top, 0, 0);
		view.setLayoutParams(layoutparams);
	}

	/**
	 * 停止的方法 对外提供
	 */
	public void stop() {
		isRefreshing = false;
		this.heardRefreshListener.complete();
		close(mPullHeight);
	}

	/**
	 * 是控件上移，关闭下拉刷新
	 *
	 * @param AbsoluteY
	 */
	private void close(float AbsoluteY) {
		ValueAnimator animator = ValueAnimator.ofFloat(AbsoluteY, 0);
		animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
			@Override
			public void onAnimationUpdate(ValueAnimator animation) {
				float top = (float) animation.getAnimatedValue();
				move(childViews, (int) top);
			}
		});
		animator.setDuration(500);
		animator.start();
	}

	/**
	 * 判断子控件是否有scrollview
	 *
	 * @return
	 */
	private boolean canChildScrollUp() {
		if (childViews == null) {
			return false;
		}
		return ViewCompat.canScrollVertically(childViews, -1);
	}

	/**
	 * hearedView回调接口
	 *
	 * @author huaxiaolin_pc
	 *
	 */
	public interface HeardViewRefreshListener {
		void down();

		void move(float subY, float countY);

		void up();

		void reFreshing();

		void complete();
	}

	public interface MyRefreshListener {
		void reFreshing();
	}
}
